from curl_cffi import requests
import json
import time
import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from collections import OrderedDict



def generate_xl(page):
    key = b'jo8j9wGw%6HbxfFn'
    iv = b'0123456789ABCDEF'

    timestamp = int(time.time() * 1000)  # 毫秒时间戳
    params = {
        "page": page,
        "_ts": timestamp
    }

    json_string = json.dumps(params, separators=(',', ':'))

    cipher = AES.new(key, AES.MODE_CBC, iv)

    # 对明文进行PKCS7填充并加密
    padded_data = pad(json_string.encode('utf-8'), AES.block_size)
    encrypted_bytes = cipher.encrypt(padded_data)

    # 将加密结果转换为十六进制字符串
    xl = encrypted_bytes.hex()

    return xl



url = 'https://mashangpa.com/api/problem-detail/5/data/'

session = requests.Session(impersonate='chrome100')

session.headers.update({
    'cookie': 'sessionid=knisj2ex95xlec3gp1rwa8zg5sfpd0wk;'
    'Hm_lvt_0d2227abf9548feda3b9cb6fddee26c0=1761963260,1761970185;'
    'HMACCOUNT=0D6738410FD188BD;'
    'Hm_lpvt_0d2227abf9548feda3b9cb6fddee26c0=1761977516',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36'

})

page = 1
total = 0
while True:
    time.sleep(1)
    print(f'正在爬取第{page}页')
    xl = generate_xl(page)

    resp = session.post(url, json={'xl': xl})

    print(f'第{page}页数据为：{resp.json()['current_array']}')
    for e in resp.json()['current_array']:
        total += e


    if resp.json()['pagination']['has_next']:
        print(f'第{page}页已爬取完毕,目前总数为{total}')
        page += 1
    else:
        print(f'数据爬取完毕，总数为：{total}')
        break

